﻿using System;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.Globalization;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;

public partial class Reports_Till_OnLine : System.Web.UI.Page
{

    #region Members

    DataTable tblTmp = new DataTable("tblTmp");
    DataTable tbl = new DataTable("tbl");
    float summarise = 0;
    string sortStr = "";
    public string sortOld
    {
        get { return (string)ViewState["sortOld"]; }
        set { ViewState["sortOld"] = value; }
    }
    public bool sortDesc
    {
        get { return (bool)ViewState["sortDesc"]; }
        set { ViewState["sortDesc"] = value; }
    }

    #endregion

    #region App Settings
    public string GetAppSetting(String mName)
    {
        String vStr = null;
        string strPath =
@"C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(strPath);
        XmlNode node = xmlDoc.SelectSingleNode(@"/configuration/appSettings");
        foreach (XmlNode keynode in node.ChildNodes)
        {
            string keyStr = keynode.Attributes["key"].Value;
            if (keyStr == mName)
            {
                vStr = keynode.Attributes["value"].Value;
                break;
            }
        }
        if (String.IsNullOrEmpty(vStr))
            vStr = "";
        return vStr;
    }
    #endregion App Settings

    #region Common

    string ReturnNumericStringFormat(float mFloat)
    {
        //string tTemp1 = mFloat.ToString();
        string tTemp1 = String.Format("{0:0.00}", mFloat);
        int tLen = tTemp1.Length;
        int tCount = (tLen - 3) / 3;
        if ((tLen - 3 - 3 * tCount) == 0) tCount--;
        int tStart = tLen - 3 - 3 * tCount;
        string tRet = tTemp1.Substring(0, tStart);
        for (int i = 0; i < tCount; i++)
        {
            tRet += " ";
            tRet += tTemp1.Substring(tStart + i * 3, 3);
        }
        //tRet += tTemp1.Substring(tStart + tCount * 3, 3);
        return tRet;
    }

    #endregion

    #region Events Forms

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sortOld = "-";
            clrDate.SelectedDate = DateTime.Today;
            getTills(null, null);
        }
    }

    protected void clrDate_SelectionChanged(Object sender, EventArgs e)
    {
        //Label1.Text = clrDate.SelectedDate.ToString("yyyy-MM-dd");
        getTills(null, null);
    }    

    #endregion

    #region Events GV
 
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        //Lerror.Text += e.Row.Cells.Count;

        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                cell.BackColor = Color.DarkBlue;
                cell.ForeColor = Color.WhiteSmoke;
            }
        }
        else
        {
            e.Row.Cells[0].BackColor = Color.DarkBlue;
            e.Row.Cells[0].ForeColor = Color.WhiteSmoke;
            e.Row.Cells[1].BackColor = Color.FromArgb(0, 255, 255, 210);
            e.Row.Cells[1].ForeColor = Color.DarkBlue;

            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        sortStr = e.SortExpression;
        if (string.IsNullOrEmpty(sortStr))
        {
            sortDesc = false;
            return;
        }
        if (sortStr == sortOld)
            sortDesc = !sortDesc;
        else
            sortDesc = false;
        sortOld = e.SortExpression;
        getTills(null, null);
    }

    #endregion

    #region Data

    protected void getTills(object sender, EventArgs e)
    {

        Ko2.DB.MySql o = new Ko2.DB.MySql();
        DateTime dt = clrDate.SelectedDate;
        tblTmp = new DataTable("tblTmp");
        tbl = new DataTable("tbl");
        Exception err = o.GetTradeOnline(ref tblTmp, dt);
        if (err != null)
        {
            Response.Write("Ошибка : " + err.Message);
            GridView1.Visible = false;
            return;
        }

        GridView1.Visible = true;

        tbl = tblTmp.Copy();
        DataColumn col = tbl.Columns.Add("N", typeof(int));
        col.SetOrdinal(0);

        if (!String.IsNullOrEmpty(sortStr))
        {
            string s = sortStr;
            if (!String.IsNullOrEmpty(s))
            {
                if (sortDesc)
                    s += " DESC";
            }
            tbl.DefaultView.Sort = s;
        }

        GridView1.DataSource = tbl;

        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            summarise += float.Parse(e.Row.Cells[6].Text);
            for (int i = 2; i <= 6; i++)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Cells[i].Text = ReturnNumericStringFormat(float.Parse(e.Row.Cells[i].Text));
            }
        }
        if (e.Row.RowIndex == -1)
        {
            Label1.Font.Bold = true;
            Label1.ForeColor = System.Drawing.Color.Green;
            string lesum = String.Empty;
            try
            {
                lesum = ReturnNumericStringFormat(summarise).ToString() + " рублей.";
            }
            catch (Exception eee) { Label1.Text = eee.ToString(); }
            Label1.Text = "Итого: " + lesum;
        }

    }

    #endregion

}